package com.ruoyi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.BoneCheck;
import com.ruoyi.domain.BoneCheck2;
import com.ruoyi.mapper.BoneCheck2Mapper;
import com.ruoyi.mapper.BoneCheckMapper;
import com.ruoyi.service.BoneCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.stream.Collectors;
@RestController
@Api(tags = "骨密度检查统计")
@SaCheckLogin

@RequestMapping(value = "/ray/bone_check_bill/")
public class ApiBoneCheckBillController extends BaseController {


    @Autowired
    private BoneCheck2Mapper boneCheck2Mapper;




    @SaIgnore
    @PostMapping("get1")
    @ApiOperation("获取统计1")
    public AjaxResult 获取统计1(@RequestParam(name = "beginTime", defaultValue = "", required = false) String beginTime,
                                      @RequestParam(name = "endTime", defaultValue = "", required = false) String endTime,
                                      @RequestParam(name = "mode1", defaultValue = "1", required = false) Integer mode1,
                                      @RequestParam(name = "mode2", defaultValue = "1", required = false) Integer mode2){
        System.out.println("beginTime = " + beginTime);
        System.out.println("endTime = " + endTime);


        QueryWrapper<BoneCheck2> 公共条件 = new QueryWrapper<>();


        if(beginTime.isEmpty()){
            AjaxResult.error("请传递开始时间");
        }

        if(endTime.isEmpty()){
            AjaxResult.error("请传递结束时间");
        }


        long beginTimeInt = DateUtil.parse(beginTime).getTime()/1000;
        long endTimeInt = DateUtil.parse(endTime).getTime()/1000;
        System.out.println("endTimeInt = " + endTimeInt);
        System.out.println("beginTimeInt = " + beginTimeInt);


        公共条件.between("time",beginTimeInt,endTimeInt);


        QueryWrapper 儿童条件 = 公共条件.clone();
        QueryWrapper 公共条件2 = 公共条件.clone();
        QueryWrapper 成人条件 = 公共条件.clone();
        成人条件.ge("age",18);
        儿童条件.between("age",0,18);

        QueryWrapper 儿童男条件 = (QueryWrapper) 儿童条件.clone();
        QueryWrapper 儿童女条件 = (QueryWrapper) 儿童条件.clone();

        儿童男条件.eq("sex", "男");
        儿童女条件.eq("sex", "女");


        QueryWrapper 成人男条件 = (QueryWrapper) 成人条件.clone();
        QueryWrapper 成人女条件 = (QueryWrapper) 成人条件.clone();

        QueryWrapper 成人骨值正常条件 = (QueryWrapper) 成人条件.clone();
        QueryWrapper 成人骨值减少条件 = (QueryWrapper) 成人条件.clone();
        QueryWrapper 成人骨值疏松条件 = (QueryWrapper) 成人条件.clone();
        QueryWrapper 成人严重骨值疏松条件 = (QueryWrapper) 成人条件.clone();


        QueryWrapper 儿童女骨值正常条件 = (QueryWrapper) 儿童条件.clone();
        QueryWrapper 儿童女骨值减少条件 = (QueryWrapper) 儿童条件.clone();
        QueryWrapper 儿童女骨值疏松条件 = (QueryWrapper) 儿童条件.clone();
        QueryWrapper 儿童女严重骨值疏松条件 = (QueryWrapper) 儿童条件.clone();


        QueryWrapper 儿童男骨值正常条件 = (QueryWrapper) 儿童条件.clone();
        QueryWrapper 儿童男骨值减少条件 = (QueryWrapper) 儿童条件.clone();
        QueryWrapper 儿童男骨值疏松条件 = (QueryWrapper) 儿童条件.clone();
        QueryWrapper 儿童男严重骨值疏松条件 = (QueryWrapper) 儿童条件.clone();





        成人男条件.eq("sex", "男");
        成人女条件.eq("sex", "女");


        成人骨值正常条件.eq("dcm_diagnosis", "骨值正常");
        成人骨值减少条件.eq("dcm_diagnosis", "骨值减少");
        成人骨值疏松条件.eq("dcm_diagnosis", "骨值疏松");
        成人严重骨值疏松条件.eq("dcm_diagnosis", "严重骨值疏松");


        儿童女骨值正常条件.eq("dcm_diagnosis", "骨值正常");
        儿童女骨值减少条件.eq("dcm_diagnosis", "骨值减少");
        儿童女骨值疏松条件.eq("dcm_diagnosis", "骨值疏松");
        儿童女严重骨值疏松条件.eq("dcm_diagnosis", "严重骨值疏松");


        儿童男骨值正常条件.eq("dcm_diagnosis", "骨值正常");
        儿童男骨值减少条件.eq("dcm_diagnosis", "骨值减少");
        儿童男骨值疏松条件.eq("dcm_diagnosis", "骨值疏松");
        儿童男严重骨值疏松条件.eq("dcm_diagnosis", "严重骨值疏松");




        List<BoneCheck2> 公共条件列表 =  boneCheck2Mapper.selectList(公共条件);

        Map<String,Object> data = new HashMap<>();

        if(mode1==1){

            // 使用Java 8的Stream和Collectors提取不同的身份证号
            List<String> 不同的身份证列表 = 公共条件列表.stream()
                    .map(BoneCheck2::getIdCard)
                    .distinct()
                    .collect(Collectors.toList());

//            for (String cardNumber : 不同的身份证列表) {
//                System.out.println(cardNumber);
//            }

            List<Date> 不同的日期 = 公共条件列表.stream()
                    .map(BoneCheck2::getDate)
                    .distinct()
                    .collect(Collectors.toList());
            List<String> 不同的日期列表 = null;
            for (Date temp:不同的日期){
//                System.out.println(temp);
                if(null!=temp){
                    try{
                        System.out.println(DateUtil.formatDate(temp));
//                        不同的日期列表.add(DateUtil.formatDate(temp));
                    }catch (Exception e){
                        return AjaxResult.error("不同的日期列表"+e.getMessage());
                    }

                }

            }

            公共条件.select("id_card", "date")
                    .groupBy("id_card", "date")
//                    .select("count(1)");
                    .select("count(*) as cnt"); // 使用count函数统计记录数，并将结果命名为cnt



            List<Map<String, Object>> results = boneCheck2Mapper.selectMaps(公共条件);

            // 然后您可以处理查询结果
            for (Map<String, Object> result : results) {
                String idCard = (String) result.get("id_card");
                Date date = (Date) result.get("date");
                Long count = (Long) result.get("cnt");
                // 处理提取的idCard, date和count数据
                System.out.println("count = " + count);
                System.out.println("date = " + DateUtil.formatDate(date));
                System.out.println("idCard = " + idCard);
            }


            List<BoneCheck2> idCardList = boneCheck2Mapper.selectList(公共条件2);
            for (BoneCheck2 temp:idCardList){
                System.out.println(temp);
            }
//            data.put("all2",boneCheck2Mapper.selectMaps(公共条件));

            data.put("all",results.size());
            data.put("ertong",boneCheck2Mapper.selectCount(儿童条件));

            data.put("ertong_nan",boneCheck2Mapper.selectCount(儿童男条件));
            data.put("ertong_nv",boneCheck2Mapper.selectCount(儿童女条件));
            data.put("chengren_nan",boneCheck2Mapper.selectCount(成人男条件));
            data.put("chengren_nv",boneCheck2Mapper.selectCount(成人女条件));

            data.put("chengren_zhengchang",boneCheck2Mapper.selectCount(成人骨值正常条件));
            data.put("chengren_jianshao",boneCheck2Mapper.selectCount(成人骨值减少条件));
            data.put("chengren_shusong",boneCheck2Mapper.selectCount(成人骨值疏松条件));
            data.put("chengren_yanzhong_shusong",boneCheck2Mapper.selectCount(成人严重骨值疏松条件));

            data.put("ertong_nan_zhengchang",boneCheck2Mapper.selectCount(儿童男骨值正常条件));
            data.put("ertong_nan_jianshao",boneCheck2Mapper.selectCount(儿童男骨值减少条件));
            data.put("ertong_nan_shusong",boneCheck2Mapper.selectCount(儿童男骨值疏松条件));
            data.put("ertong_nan_yanzhong_shusong",boneCheck2Mapper.selectCount(儿童男严重骨值疏松条件));

            data.put("ertong_nv_zhengchang",boneCheck2Mapper.selectCount(儿童女骨值正常条件));
            data.put("ertong_nv_jianshao",boneCheck2Mapper.selectCount(儿童女骨值减少条件));
            data.put("ertong_nv_shusong",boneCheck2Mapper.selectCount(儿童女骨值疏松条件));
            data.put("ertong_nv_yanzhong_shusong",boneCheck2Mapper.selectCount(儿童女严重骨值疏松条件));
        }else{


            data.put("all",boneCheck2Mapper.selectCount(公共条件));
            data.put("ertong",boneCheck2Mapper.selectCount(儿童条件));
            data.put("ertong_nan",boneCheck2Mapper.selectCount(儿童男条件));
            data.put("ertong_nv",boneCheck2Mapper.selectCount(儿童女条件));
            data.put("chengren_nan",boneCheck2Mapper.selectCount(成人男条件));
            data.put("chengren_nv",boneCheck2Mapper.selectCount(成人女条件));

            data.put("chengren_zhengchang",boneCheck2Mapper.selectCount(成人骨值正常条件));
            data.put("chengren_jianshao",boneCheck2Mapper.selectCount(成人骨值减少条件));
            data.put("chengren_shusong",boneCheck2Mapper.selectCount(成人骨值疏松条件));
            data.put("chengren_yanzhong_shusong",boneCheck2Mapper.selectCount(成人严重骨值疏松条件));

            data.put("ertong_nan_zhengchang",boneCheck2Mapper.selectCount(儿童男骨值正常条件));
            data.put("ertong_nan_jianshao",boneCheck2Mapper.selectCount(儿童男骨值减少条件));
            data.put("ertong_nan_shusong",boneCheck2Mapper.selectCount(儿童男骨值疏松条件));
            data.put("ertong_nan_yanzhong_shusong",boneCheck2Mapper.selectCount(儿童男严重骨值疏松条件));

            data.put("ertong_nv_zhengchang",boneCheck2Mapper.selectCount(儿童女骨值正常条件));
            data.put("ertong_nv_jianshao",boneCheck2Mapper.selectCount(儿童女骨值减少条件));
            data.put("ertong_nv_shusong",boneCheck2Mapper.selectCount(儿童女骨值疏松条件));
            data.put("ertong_nv_yanzhong_shusong",boneCheck2Mapper.selectCount(儿童女严重骨值疏松条件));
        }







        return AjaxResult.success(beginTime+"_"+endTime,data);
    }

}
